﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class RoleDao
    {


        public RoleDao()
        {

            Mapper.CreateMap<E_Role, RoleModel>();
        }
        public List<E_Role> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Role.Count();

                return edm.E_Role.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public RoleModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Role.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Role, RoleModel>(query);
            }

        }


        public int Save(RoleModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Role.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Role();

                    edm.AddToE_Role(query);

                }


                query.RoleName = obj.RoleName;

                query.RoleDesc = obj.RoleDesc;

                query.IsUsed = obj.IsUsed;
                query.CreateUserId = obj.CreateUserId;
                query.OrderId = obj.OrderId;
                query.Funcs = obj.Funcs;
                edm.SaveChanges();

                return query.Id;


            }

        }
        public List<RoleModel> GetAll()
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_Role>, List<RoleModel>>(edm.E_Role.ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Role.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

